package com.example.nacosconsumer.Config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyMvcConfig implements WebMvcConfigurer{
    /**
     * 配置拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册一个拦截器，检查会话，所有/开头的请求都经过此拦截器
        registry.addInterceptor(new HandlerInterceptor() {
                }).addPathPatterns("/**").
                excludePathPatterns("/login","/static/**","/user/**","/zhuce","/rest","/forum/uploadFile",
                //swagger3需要放行的界面
                "/swagger-resources/**","/swagger-ui/**", "/v3/**", "/error"
               );
    }

    /**
     * 配置资源映射
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {  //配置静态资源的映射
        // 设置图片上传的绝对路径
        // 用于解决关于图片上传后需要重启服务器才能刷新图片的问题
        String path="F:\\ZSB\\ziliao\\SpringCloudTest\\Comsumer\\src\\main\\resources\\pictures\\";
        String cloudpath = "/root/springcloud/pictures/";

        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:static/");
        registry.addResourceHandler("/templates/**")
                .addResourceLocations("classpath:templates/");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:\\META-INF\\resources\\");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:\\META-INF\\resources\\webjars\\");
        registry.addResourceHandler("/picture/**")
                .addResourceLocations("file:"+path);
        registry.addResourceHandler("/pictures/**")
                .addResourceLocations("file:"+cloudpath);

    }

    /**
     * 请求页面映射
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/templates/zhuce.html").setViewName("zhuce");
        registry.addViewController("/templates/login.html").setViewName("login");
    }

}
